/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kurvykurvy.actions;

import com.kurvykurvy.image.ImageLoader;
import com.kurvykurvy.image.LoadedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import com.kurvykurvy.ui.filter.JPEGFileFilter;

/**
 *
 * @author HY
 */
public class LoadImageAction
{
    public LoadImageAction()
    {
    }

    public void loadImages()
    {
        JFileChooser fileChooser = new JFileChooser();

        fileChooser.setMultiSelectionEnabled( true );
        fileChooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES );
        fileChooser.setFileFilter( new JPEGFileFilter() );

        int option = fileChooser.showDialog( fileChooser, "Select" );
        if( JFileChooser.APPROVE_OPTION == option )
        {
            File[] files = fileChooser.getSelectedFiles();
            for( int i = 0; i < files.length; i++ )
            {
                try
                {
                    loadFile( files[i] );
                }
                catch ( IOException ex )
                {
                    Logger.getLogger( LoadImageAction.class.getName() ).log( Level.SEVERE, null, ex );
                }
            }
        }
    }

    public void loadFile( File target ) throws IOException
    {
        if( target.isFile() )
        {
            ImageLoader.getInstance().loadImage( target );
        }
        else
        {
            File[] files = target.listFiles(  new JPEGFileFilter() );
            for( int i = 0; i < files.length; i++ )
            {
                loadFile( files[ i ] );
            }
        }
    }
}
